#-*- encoding: utf-8 -*-

import imaplib
import email
import sqlite3

b_createtable=False

sql_con=sqlite3.connect('mail.db')
cur=sql_con.cursor()
if b_createtable:
    cur.execute('create table mail (mail_num text)')

def is_mail_read(mail_num):
    global cur
    sql='select count(*) from mail where mail_num='
    cur.execute(sql+mail_num)
    ret=cur.fetchone()
    #print ret[0]
    return int(ret[0]) > 0

def insert_mail_num(mail_num):
    global  cur
    sql='insert into mail (mail_num) values (%s)'
    cur.execute(sql % mail_num)
    sql_con.commit()

imap_con=imaplib.IMAP4('imap.263.net',143)
imap_con.login('gongming@ebara.cn','Ebara2011')

imap_con.select('INBOX')

#x,y=imap_con.status('INBOX','(MESSAGES UNSEEN)')

imap_con.literal='restartvm'
type,data=imap_con.search('utf-8','SUBJECT')

print data

for num in data[0].split():
    if not is_mail_read(num):
        insert_mail_num(num)
        print num

imap_con.close()
imap_con.logout()
sql_con.close()